{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "297a793f-099d-45f3-b64e-10fd70cace49",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "%reload_ext jupyter_ai"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "01d054df-6fed-4c53-9fd1-27e5e09d64f9",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.0"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2/2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "1d28e4d2-1677-4a3a-b489-94c95d7233ec",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "ename": "ZeroDivisionError",
     "evalue": "division by zero",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mZeroDivisionError\u001b[0m                         Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[3], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;241;43m2\u001b[39;49m\u001b[38;5;241;43m/\u001b[39;49m\u001b[38;5;241;43m0\u001b[39;49m\n",
      "\u001b[0;31mZeroDivisionError\u001b[0m: division by zero"
     ]
    }
   ],
   "source": [
    "2/0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "267decf3-9a23-4ca1-a2fa-b472c0e911c8",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "The error is a `ZeroDivisionError`. It occurred because the code attempted to divide a number by zero, which is mathematically undefined. This error message specifically states \"division by zero\", indicating that there was an attempt to divide by zero."
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "execution_count": 4,
     "metadata": {
      "text/markdown": {
       "jupyter_ai": {
        "model_id": "gpt-3.5-turbo",
        "provider_id": "openai-chat"
       }
      }
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%ai chatgpt\n",
    "Explain the following Python error:\n",
    "    {Err[3]}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "686afba1-6a0d-4046-9125-ab78e5414b70",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "You can also use the `%ai error` command, which takes the same parameters as the basic `%%ai` command, to explain the most recent error."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "a2e16281-93ee-42ca-adee-45ed842dd96f",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "Missing parentheses in call to 'print'. Did you mean print(...)? (1142230402.py, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  Cell \u001b[0;32mIn[5], line 1\u001b[0;36m\u001b[0m\n\u001b[0;31m    print 'foo'\u001b[0m\n\u001b[0m    ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m Missing parentheses in call to 'print'. Did you mean print(...)?\n"
     ]
    }
   ],
   "source": [
    "print 'foo'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "76ee2adb-afe9-4f65-95b3-9757ec1909dc",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Cell In[5], line 1\n",
      "    print 'foo'\n",
      "    ^\n",
      "SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(Err[5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "8548d2b2-f79f-4b33-8338-3819a37ba856",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "The error is a `SyntaxError`. It occurred because the code uses Python 2 syntax to print a string without parentheses, but the notebook is running in Python 3, which requires parentheses for `print` statements. \n",
       "\n",
       "The error message specifies that the issue is \"Missing parentheses in call to 'print'\". The message also suggests a possible solution by stating \"Did you mean print(...)?\" which indicates that enclosing the string \"foo\" in parentheses will resolve the syntax error."
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "execution_count": 7,
     "metadata": {
      "text/markdown": {
       "jupyter_ai": {
        "model_id": "gpt-3.5-turbo",
        "provider_id": "openai-chat"
       }
      }
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%ai error chatgpt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "f32009ae-c7d5-4649-aa13-852e193ec604",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "AI generated code inserted below &#11015;&#65039;"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "execution_count": 8,
     "metadata": {
      "text/html": {
       "jupyter_ai": {
        "model_id": "gpt-3.5-turbo",
        "provider_id": "openai-chat"
       }
      }
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%ai chatgpt --format code\n",
    "The following Python code:\n",
    "--\n",
    "{In[5]}\n",
    "--\n",
    "produced the following Python error:\n",
    "--\n",
    "{Err[5]}\n",
    "--\n",
    "Write a new version of this code that does not produce that error."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8b10227e-53b3-48e2-bba0-104cc222ffc5",
   "metadata": {},
   "outputs": [],
   "source": [
    "print('foo')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "15feb854-2e14-47d8-9327-2cbbc2aeaf2c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
